﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sami_SMS_CRM_Business;
using System.Data;

namespace Sami_SMS_CRM_UI
{
    public partial class crm_advertisment_view : BasePage
    {
        private int? adv_id
        {
            get
            {
                try
                {
                    return int.Parse(Request.QueryString["advid"]);
                }
                catch
                {
                    return null;
                }

            }
        }
        AdvertismentRepository repository = new AdvertismentRepository();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!AllowManageAdvertisment())
            {
                RedirectToPage(AccessDeniedURL);
            }
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        void paging_PagingChange(object sender, EventArgs e)
        {
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            ViewGrid();
            pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
                pagingbottom.CurrentPage = 1;
                pagingbottom.PageSize = PageSize;
                pagingbottom.PageDisplay = PageDisPlay;
                txtDateFrom.Text = DateTime.Now.ToString("MM/dd/yyyy");
                txtDateTo.Text = DateTime.Now.ToString("MM/dd/yyyy");
                ViewData();
                ViewGrid();
            }
        }

        private void ViewData()
        {
            if (adv_id == null) return;
            var adv = repository.GetById(adv_id.Value);
            if (adv != null)
            {
                ltrMessage.Text = adv.message;
                ltrMaxAdv.Text = adv.max_advertisment.ToString();
                ltrSentAdv.Text = adv.sent_advertisment.ToString();
                ltrSenderName.Text = adv.sender_name;
                ltrStartExpected.Text = adv.start_expected.ToString();
                ltrEndExpected.Text = adv.end_expected.ToString();
                ltrAllowSentFrom.Text = adv.allow_sent_from.ToString();
                ltrAllowSentTo.Text = adv.allow_sent_to.ToString();
                ltrStatus.Text = adv.status;
                var action = new ActionRepository().GetById(adv.action_id);
                if (action != null)
                {
                    ltrAction.Text = action.action_name;
                }
            }
        }

        private void ViewGrid()
        {
            if (adv_id == null) return;
            try
            {
                int currentPage = pagingbottom.CurrentPage;
                int pageSize = pagingbottom.PageSize;
                int totalRecord = 0;
                int totalMsg = 0;
                DateTime? timeFrom = txtDateFrom.Text.Trim() == string.Empty ? (DateTime?)null : DateTime.Parse(txtDateFrom.Text, DateTimeFormat);
                DateTime? timeTo = txtDateTo.Text.Trim() == string.Empty ? (DateTime?)null : DateTime.Parse(txtDateTo.Text, DateTimeFormat);
                grvStatistic.DataSource = repository.StatisticMessage(adv_id.Value, timeFrom, timeTo, currentPage, pageSize, ref totalRecord, ref totalMsg);
                grvStatistic.DataBind();
                if (grvStatistic.FooterRow != null)
                {
                    grvStatistic.FooterRow.Attributes.Add("class", "tfooter");
                    grvStatistic.FooterRow.Cells[1].Text = "<b>Total</b>";
                    grvStatistic.FooterRow.Cells[2].Text = "<b>" + totalMsg + "</b>";
                }
                pagingbottom.TotalRecord = totalRecord;
                pagingbottom.Visible = pagingbottom.TotalPage > 1;
            }
            catch (Exception e)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert(\"" + Server.HtmlEncode(e.Message) + "\");", true);
            }
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "registerScript();", true);
        }

        protected void btnStatistic_Click(object sender, EventArgs e)
        {
            pagingbottom.CurrentPage = 1;
            ViewGrid();
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            pagingbottom.PageSize = int.Parse((sender as DropDownList).SelectedValue);
            ViewGrid();
        }
    }
}